{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "engaging-soviet",
   "metadata": {},
   "source": [
    "# Indonesian VAT Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "illegal-castle",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "gross-education",
   "metadata": {},
   "source": [
    "The function `clean_id_npwp()` cleans a column containing Indonesian VAT Number (NPWP) strings, and standardizes them in a given format. The function `validate_id_npwp()` validates either a single NPWP strings, a column of NPWP strings or a DataFrame of NPWP strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "handmade-canal",
   "metadata": {},
   "source": [
    "NPWP strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"013000666091000\"\n",
    "* `standard`: NPWP strings with proper whitespace in the proper places, like \"01.300.066.6-091.000\"\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_id_npwp()` and `validate_id_npwp()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dominican-journalism",
   "metadata": {},
   "source": [
    "### An example dataset containing NPWP strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "elect-penetration",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"npwp\": [\n",
    "            \"013000666091000\",\n",
    "            \"123456789\",\n",
    "            \"51824753556\",\n",
    "            \"51 824 753 556\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\"\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aging-transition",
   "metadata": {},
   "source": [
    "## 1. Default `clean_id_npwp`\n",
    "\n",
    "By default, `clean_id_npwp` will clean npwp strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "superior-vision",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_id_npwp\n",
    "clean_id_npwp(df, column = \"npwp\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fatal-investing",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "isolated-outside",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "blank-income",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "monthly-metropolitan",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_id_npwp(df, column = \"npwp\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "coastal-development",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "loose-office",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_id_npwp(df, column = \"npwp\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "broad-track",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned NPWP strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "opening-country",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_id_npwp(df, column=\"npwp\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "intense-dominican",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "geographic-extension",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "solid-charles",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_id_npwp(df, \"npwp\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "excessive-experiment",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "broad-philadelphia",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_id_npwp(df, \"npwp\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "excess-attraction",
   "metadata": {},
   "source": [
    "## 4. `validate_id_npwp()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "north-replication",
   "metadata": {},
   "source": [
    "`validate_id_npwp()` returns `True` when the input is a valid NPWP. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_id_npwp()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_id_npwp()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_id_npwp()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "familiar-being",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_id_npwp\n",
    "print(validate_id_npwp(\"013000666091000\"))\n",
    "print(validate_id_npwp(\"123456789\"))\n",
    "print(validate_id_npwp(\"51824753556\"))\n",
    "print(validate_id_npwp(\"51 824 753 556\"))\n",
    "print(validate_id_npwp(\"hello\"))\n",
    "print(validate_id_npwp(np.nan))\n",
    "print(validate_id_npwp(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eight-junior",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adjusted-saturday",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_id_npwp(df[\"npwp\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "seasonal-strategy",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cognitive-possession",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_id_npwp(df, column=\"npwp\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "public-interval",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "human-mistress",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_id_npwp(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "infrared-superior",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
